//
//  MJChangeHeaderImageTool.m
//  MJ
//
//  Created by qiaoBu on 2017/12/11.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJChangeHeaderImageTool.h"

@implementation MJChangeHeaderImageTool


+ (NSString *)saveImageData:(NSData *)imageData toDocumentsWithFileName:(NSString *)filename{
    
    NSString *imageDir =  [NSString stringWithFormat:@"%@/TJ_UploadImageResource",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
    
    NSLog(@"imageDir -------- %@",imageDir);
    NSString *imagePath = [NSString stringWithFormat:@"%@/%@",imageDir,filename];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
    //判断文件夹存不存在
    if(!(isDirExist && isDir))
        
    {
        NSLog(@"上传图片的文件夹不存在，要进行创建");
        [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSLog(@"上传图片的文件夹存在，imagePath ----------%@",imagePath);
    [imageData writeToFile:imagePath atomically:YES];
    return  imagePath;
}

+ (void)deleteImageResourceWithPath:(NSString *)path{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSLog(@"filePath++++++++++%@",path);
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error = nil;
        [fileManager removeItemAtPath:path error:&error];
        
    });
}

@end
